package com.cndw.kungfu.model.undercity;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.kungfu.dao.MonsterDao;
import com.cndw.kungfu.dao.MonsterDeployDao;
import com.cndw.kungfu.domain.Monster;
import com.cndw.kungfu.domain.MonsterAi;
import com.cndw.kungfu.domain.MonsterDeploy;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.domain.WorldBoos;
import com.cndw.kungfu.model.battle.FighterMonster;

/**
 * 地下城管理
 * 
 * @author fantadust
 * 
 */
@Component
public class UndercityManager {
	@Autowired
	private MonsterDao monsterDao;
	@Autowired
	private MonsterDeployDao monsterDeployDao;

	/**
	 * 为某个刷怪点初始化怪物
	 * 
	 * @param monsterDeployId
	 * @return
	 */
	public List<FighterMonster> initMonstersByDeploy(int monsterDeployId, int addMonster) {
		MonsterDeploy monsterDeploy = Context.getUndercityServer().getDeployById(monsterDeployId);
		if (monsterDeploy == null)
			return null;
		List<Integer> monsterIdsList = monsterDeploy.getMonsterIdsList();
		if (monsterIdsList == null) {
			monsterIdsList = new LinkedList<Integer>();
		}
		if (monsterIdsList.contains(monsterDeploy.getDefaultMonsterId()) == false) {
			monsterIdsList.add(monsterDeploy.getDefaultMonsterId());
		}

		List<FighterMonster> monsters = new LinkedList<FighterMonster>();
		if (monsterDeploy.getRefreshApp() > 0) {
			WorldBoos worldBoos = WorldBoos.getById(monsterDeploy.getRefreshApp());
			if (worldBoos != null && worldBoos.getBossFighter() != null) {
				monsters.add(worldBoos.getBossFighter());
			}
			return monsters;
		}

		//RequestHandler.log.debug(monsterIdsList.size());
		Monster e = null;
		for (Integer i : monsterIdsList) {
			e = Context.getUndercityServer().getMonster(i);
			if (e == null) {
				continue;
			}

			monsters.add(e.fighter());
		}
		while (addMonster > 0 && e != null) {
			addMonster--;
			monsters.add(e.fighter());
		}

		return monsters;

	}

	public List<Monster> getAllMonstersFromDb() {
		return monsterDao.getMonsters();
	}

	public List<MonsterDeploy> getAllMonsterDeploysFromDb() {
		try {
			return monsterDeployDao.getAllMonsterDeploy();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public List<MonsterAi> getAllMonsterAisDB() {
		try {
			return monsterDao.getMonsterAis();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
